package com.moonshineproject.plugin.console.setting
{
	import com.moonshineproject.plugin.settings.ISettingsProvider;
	import com.moonshineproject.plugin.settings.vo.AbstractSetting;
	
	import flash.ui.Keyboard;
	
	import mx.core.IVisualElement;
	
	public class SpecialKeySetting extends AbstractSetting
	{
		[Bindable]
		public var eventPropName:String;
		
		private var _eventValue:int;
		[Bindable]
		public function get eventValue():int
		{
			return _eventValue;
		}
		public function set eventValue(v:int):void
		{
			_eventValue = v;
			setPendingSetting(null);
		}
		
		public function SpecialKeySetting(provider:ISettingsProvider, name:String, label:String)
		{
			super();
			this.provider = provider;
			this.name = name;
			this.label = label; 
			defaultValue = stringValue;
		}
		
		public function getKeyAsChar():String
		{
			if (eventPropName == 'charCode')
			{
				return String.fromCharCode(eventValue);
			}
			
			return "?";
		}
		
		override public function get renderer():IVisualElement
		{
			var rdr:SpecialKeyRenderer = new SpecialKeyRenderer();
			rdr.setting = this;
			return rdr;
		}
	
		override public function set stringValue(v:String):void 
		{
			if (v != "")
			{
				var values:Array = v.split(":");
				eventPropName = values[0];
				eventValue = parseInt(values[1]);
			}
			setPendingSetting(v);
		}		
		
		override protected function getSetting() : *{
			if (!provider) return "";
			eventPropName = provider['consoleTriggerKeyPropertyName'];
			eventValue = provider['consoleTriggerKeyValue'];
			return eventPropName + ':' + eventValue;			
		}
		override protected function setPendingSetting(v:*):void 
		{
				if (!provider) return;
				provider['consoleTriggerKeyPropertyName'] = eventPropName;
				provider['consoleTriggerKeyValue'] = eventValue;
		}
		
	}
}